<?php
/**
 * stack item
 * provide a manipulatable array
 * with set/append/prepend/remove/order functions
 */
class Magnum_Utility_Stack_Item
{
    /**
     * first of all, an array to store the item info
     * @var array
     */
    protected $_items = array();


    /**
     * constructor
     * @param str $name the name of this stack item - optional - can be used for forcing singlton later if set.
     * @return void
     */
    public function __construct($name = null)
    {

    }// end __construct


    /**
     * debug function
     */
    public function _dump()
    {
        echo '<pre>' . htmlentities(print_r($this, 1)) . '</pre>';

    }// end _dump


    /**
     * get items...
     * @return array
     */
    public function getItems()
    {
        return $this->_items;

    }// end getItems

    /*-------------------------------[public apis]-------------------------------*/

    /**
     * add a new object in
     * @param mixed $item anything, str/int/obj...
     * @param mixed $key  the position, if null, add to the bottom
     * @param str   $type the type of operation
     * @return $this
     */
    public function set($item, $key = '', $type = 'OVERWRITE')
    {
        switch ($type) {
            case 'APPEND':
                return $this->_append($item, $key);
                break;
            case 'PREPEND':
                return $this->_prepend($item, $key);
                break;
            case 'OVERWRITE':
            default:
                return $this->_set($item, $key);
                break;
        }

    }// end set


    /**
     * append item to bottom of array
     * @param mixed $item the item to append
     * @param int $key the position
     * @return $this
     */
    public function _append($item, $key = null)
    {
        if (empty($key)) {
            $item = array($item);
        } else {
            $item = array($key => $item);
        }
        $this->_items = array_merge($this->_items, $item);
        return $this;

    }// end _append


    /**
     * prepend item to top of array
     * @param mixed $item the item to append
     * @param int $key the position
     * @return $this
     */
    public function _prepend($item, $key = null)
    {
        if (empty($key)) {
            $item = array($item);
        } else {
            $item = array($key => $item);
        }
        $this->_items = array_merge($item, $this->_items);
        return $this;

    }// end _prepend


    /**
     * remove item by pos
     * @param mixed $key the position
     * @return $this
     */
    public function remove($key = null)
    {
        if (isset($this->_items[$key])) {
            unset($this->_items[$key]);
        }

        if (empty($key)) {
            // remove all
            $this->_items = null;
        }

        return $this;

    }// end remove

    /**
     * get item by pos
     * @param mixed $key the position
     * @return mixed
     */
    public function get($key)
    {
        if (isset($this->_items[$key])) {
            return $this->_items[$key];
        }

        return null;

    }// end get


    /*-------------------------------[protected apis]-------------------------------*/

    /**
     * simple set...
     * @param mixed $item anything, str/int/obj...
     * @param mixed $key the position
     * @return $this
     */
    protected function _set($item , $key)
    {
        if (empty($key)) {
            // add to the end
            $key = count($this->_items);
        }
        $this->_items[$key] = $item;
        return $this;

    }// end _set

    /*-------------------------------[to string]-------------------------------*/

    /**
     * flatten the whole thing
     */
    public function __toString()
    {
        // only string items are stored...
        $tmp = '';
        foreach ($this->_items as $item) {
            if (is_string($item) || is_numeric($item)) {
                $tmp .= $item . ' '; // add space so it's not jammed up
            }
        }
        return $tmp;

    }// end __toString


    /**
     * export
     * @param bool $print if true, print
     */
    public function export($print = 1)
    {
        // if empty
        if (empty($this->_items)) {
            $this->_items = array();
        }

        // treat as all string
        $tmp = implode(EOL, $this->_items) . EOL;
        if ($print) {
            echo $tmp;
            return;
        }

        return $tmp;

    }// end export

}// end Magnum_Utility_items_Item
?>